import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage'
Vue.use(Vuex)
const TOKEN_KEY = 'TOUTIAO_USER'
const USER_KEY = 'USERINFO'
export default new Vuex.Store({
  state: {
    // 用户的token
    token: getItem(TOKEN_KEY) || null,
    userInfo: null
  },
  mutations: {
    // 设置token
    setUser(state, data) {
      // 设置给state
      state.token = data
      // state.userInfo = data.userInfo
      // 存储到本地  
      setItem(TOKEN_KEY, data)
    },
    setUserInfo(state, data) {
      // 设置给state
      // state.user = data.userInfo
      state.userInfo = data
      // 存储到本地  
      setItem(USER_KEY, data)
    },
    // 清空token
    removeUser(state, data) {
      state.token = data
    },
    // 清空userInfo
    removeUserInfo(state, data) {
      state.userInfo = data
    }
  },

  // 建立快捷访问
  getters: {
    token: state => {
      return state.token ? state.token : undefined
    },
    userInfo: state => state.userInfo
  },
  actions: {
  },
  modules: {
  }
})
